package cc.rengu.igas.momp.common.dao.impl;

import cc.rengu.igas.momp.common.dao.TMTxnOrderDetMapper;
import cc.rengu.igas.momp.common.entity.TMTxnOrderDet;
import cc.rengu.jradp.mods.dbs.Database;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 订单明细表数据库操作接口实现
 *
 * @author Jinan Liu
 * @version 1.0.0
 * @date 2020/4/17 10:20
 */

public class TMTxnOrderDetMapperImpl implements TMTxnOrderDetMapper {

    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    private String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppParamConstant.DATABASE_CONF_NODE, AppConfigConstant.MOMP_POOL_NAME);


    /**
     * 新增单条订单明细数据库操作接口实现
     *
     * @param tmTxnOrderDet 订单明细表
     * @return int 返回码
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/4/17 10:28
     */
    @Override
    public int insertSingleRecordIntoTMTxnOrderDet(TMTxnOrderDet tmTxnOrderDet) throws Exception {

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "insertSingleRecordIntoTMTxnOrderDet");

        int iReturnCode = dbsUtil.dbsExecuteTransactionSql(sqlStr, tmTxnOrderDet, TMTxnOrderDet.class);
        if (Database.DBS_SUCCESS != iReturnCode) {
            rglog.error("法人机构号<{}>流水号<{}>新增订单明细数据失败! RETURN_CODE=<{}>", tmTxnOrderDet.getInstId(), tmTxnOrderDet.getOrderId(), iReturnCode);
            return iReturnCode;
        }

        return Database.DBS_SUCCESS;
    }

    /**
     * 新增多条订单明细数据库操作接口实现
     *
     * @param tmTxnOrderDetList 订单明细表
     * @return int 返回码
     * @throws Exception 异常信息
     * @author liujinan
     * @date 2020/6/4 11:45
     */
    @Override
    public int insertTMTxnOrderDetList(List<TMTxnOrderDet> tmTxnOrderDetList) throws Exception {

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "insertSingleRecordIntoTMTxnOrderDet");

        for (TMTxnOrderDet tmTxnOrderDet : tmTxnOrderDetList) {
            int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, tmTxnOrderDet, TMTxnOrderDet.class);
            if (Database.DBS_SUCCESS != iReturnCode) {
                rglog.error("法人机构号<{}>流水号<{}>新增订单明细数据失败! RETURN_CODE=<{}>", tmTxnOrderDet.getInstId(), tmTxnOrderDet.getOrderId(), iReturnCode);
                return iReturnCode;
            }
        }
        return Database.DBS_SUCCESS;
    }


    /**
     * 查询原交易订单明细信息数据库接口定义
     *
     * @param instId        法人机构号
     * @param origTransDate 原交易日期
     * @param origTraceNo   原交易流水号
     * @return java.util.List<cc.rengu.igas.momp.common.entity.TMTxnOrderDet> 订单明细表
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/4/26 11:13
     */
    @Override
    public List<TMTxnOrderDet> selectOriginalTransactionOrderDetailListInfo(String instId, String origTransDate, String origTraceNo) throws Exception {

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectOriginalTransactionOrderDetailListInfo");

        // 按接口赋查询条件
        TMTxnOrderDet tmTxnOrderDet = new TMTxnOrderDet();
        tmTxnOrderDet.setInstId(instId);
        tmTxnOrderDet.setTxnDate(origTransDate);
        tmTxnOrderDet.setFrontSeqNum(origTraceNo);

        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, tmTxnOrderDet, TMTxnOrderDet.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("查询机构<{}>在<{}>的流水号<{}>对应的订单交易明细失败!", instId, origTransDate, origTraceNo);
            return null;
        }

        return objectList.stream().map(item -> (TMTxnOrderDet) (item)).collect(Collectors.toList());
    }

    /**
     * 更新单条订单明细记录状态数据库接口定义
     *
     * @param tmTxnOrderDet 订单明细表
     * @return int 返回码
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/6/11 21:23
     */
    @Override
    public int updateSingleOriginalTransactionOrderDetailStatusInfo(TMTxnOrderDet tmTxnOrderDet) throws Exception {

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updateSingleOriginalTransactionOrderDetailStatusInfo");

        int iReturnCode = dbsUtil.dbsExecuteTransactionSql(sqlStr, tmTxnOrderDet, TMTxnOrderDet.class);
        if (Database.DBS_SUCCESS != iReturnCode) {
            rglog.error("法人机构号<{}>流水号<{}>更新订单明细数据失败! RETURN_CODE=<{}>", tmTxnOrderDet.getInstId(), tmTxnOrderDet.getOrderId(), iReturnCode);
            return iReturnCode;
        }

        return Database.DBS_SUCCESS;
    }


    /**
     * 更新订单明细信息数据库接口定义
     *
     * @param tmTxnOrderDet 订单明细表
     * @return int 返回码
     * @throws Exception 异常信息
     * @author liujinan
     * @date 2020/5/14
     */
    @Override
    public int updateTmTxnOrderDetByKey(TMTxnOrderDet tmTxnOrderDet) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "mompUpdateTmTxnOrderDetByKey");

        int iReturnCode = dbsUtil.dbsExecuteTransactionSql(sqlStr, tmTxnOrderDet, TMTxnOrderDet.class);
        if (Database.DBS_SUCCESS != iReturnCode) {
            rglog.error("更新法人机构号<{}>在<{}>的流水号<{}>业务状态失败! RETURN_CODE=<{}>", tmTxnOrderDet.getInstId(), tmTxnOrderDet.getTxnDate(), tmTxnOrderDet.getFrontSeqNum(), iReturnCode);
            return iReturnCode;
        }

        return Database.DBS_SUCCESS;
    }


    /**
     * 查询用户已参与某个营销活动次数
     *
     * @param userId 用户编号
     * @param mcNo   营销活动编号
     * @throws Exception 异常信息
     * @author liujinan
     * @since 2020/9/7 10:10
     */
    @Override
    public List<TMTxnOrderDet> selectUserOrderDetailListInfo(String instId, String txnDate, String userId, String mcNo) throws Exception {

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectUserOrderDetailListInfo");

        // 按接口赋查询条件
        TMTxnOrderDet tmTxnOrderDet = new TMTxnOrderDet();
        tmTxnOrderDet.setInstId(instId);
        tmTxnOrderDet.setTxnDate(txnDate);
        tmTxnOrderDet.setUserId(userId);
        tmTxnOrderDet.setMcNo(mcNo);

        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, tmTxnOrderDet, TMTxnOrderDet.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("查询机构<{}> 日期<{}> 用户<{}> 营销活动<{}> 订单交易明细信息失败!", instId, txnDate, userId, mcNo);
            return null;
        }

        return objectList.stream().map(item -> (TMTxnOrderDet) (item)).collect(Collectors.toList());
    }
}
